﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizShpChargeAlipayLog : CustBizObject
    {
        private CDmShpChargeAlipayLog m_CDmShpChargeAlipayLog = new CDmShpChargeAlipayLog();
        private CDmShpChargeAlipayLog oDmShpChargeAlipayLog
        {
            get { return m_CDmShpChargeAlipayLog; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizShpChargeAlipayLog()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmShpChargeAlipayLog.SHP_CHARGE_ALIPAY_LOG);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ALIPAY_LOG_ID"]; 
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            //if (drw["TRADING_NO"].ToString() == string.Empty)
            //{
            //    drw["TRADING_NO"] = msBsc.SvRef.ApplicationConst.Const.GetNewSerialNo("TRADING_NO", System.DateTime.Now.Year.ToString(), 8);
            //}
            //drw["BUYER_DEL"] = false;
            //drw["BUYER_RATE_SID"] = "";

            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            } 
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        { 
            return true;
        }

        public DataSet SelectByIds(string strIdS)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE  " + this.KeyFieldName + " IN  ('" + strIdS.Replace("'", "").Replace(",", "','") + "')");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectByChargeTradingId(string strChargeTradingId)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE CHARGE_TRADING_ID='" + strChargeTradingId + "' ORDER BY INITIAL_DT ASC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

        public DataSet SelectByAlipayNo(string strAlipayNo)
        {
            DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE ALIPAY_TRADING_NO='" + strAlipayNo + "' ORDER BY INITIAL_DT ASC");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }


        //public DataSet SelectForFinder(string strTRADING_NO, string strUSER_NME, string strCOMM_USER_NME)
        //{
        //    if (strUSER_NME != string.Empty || strCOMM_USER_NME != string.Empty)
        //    {
        //        strCOMM_USER_NME = "";
        //    }
        //    if (strCOMM_USER_NME != string.Empty)
        //    {
        //        DataSet ds1 = this.DataObj.QueryBySQL("SELECT * FROM SHP_COMMERCIAL_TENANT_INFO WHERE COMM_TENANT_NME='" + strCOMM_USER_NME + "' OR USER_NME='" + strCOMM_USER_NME + "'");
        //        if (ds1.Tables[0].Rows.Count > 0)
        //        {
        //            strCOMM_USER_NME = ds1.Tables[0].Rows[0]["COMM_TENANT_ID"].ToString();
        //        }
        //    }


        //    DataSet ds = this.DataObj.QueryBySQL("SELECT * FROM " + this.TableName + " WHERE (1=1)"
        //        + (strTRADING_NO == string.Empty ? "" : " AND (TRADING_NO='" + strTRADING_NO + "')")
        //        + (strUSER_NME == string.Empty ? "" : " AND (ACCOUNT_NO='" + strUSER_NME + "')")
        //        + (strCOMM_USER_NME == string.Empty ? "" : " AND (ACCOUNT_NO='" + strCOMM_USER_NME + "')")
        //        + "  ORDER BY INITIAL_DT ASC");
        //    ds.Tables[0].TableName = this.TableName;
        //    return ds;
        //}

        ///// <summary>
        ///// 交易完成后赠送积分，用户类型,0系统用户，1商户，2客户,3推广员，4代理商
        ///// </summary>
        ///// <param name="drOrder"></param>
        //public void AddFromScoreLog(DataRow drScoreLog,string strUserNme,string strPromotionNme,string strFwdNme)
        //{
        //    if (drScoreLog == null || strUserNme==string.Empty || strPromotionNme==string.Empty || strFwdNme==string.Empty )
        //    {
        //        return;
        //    } 
        //    DataSet dstrad = new CDmShpChargeAlipayLog(); 
        //    //推广员增加积分
        //    DataRow drnewtrad = dstrad.Tables[0].NewRow();
        //    Epm.AppLogic.Framework.CUtil.CopyDataRow(drScoreLog, drnewtrad);
        //    drnewtrad["SCORE_PROMOTION_ID"] = this.DataObj.NewID();
        //    drnewtrad["ACCOUNT_NO"] = strPromotionNme;
        //    drnewtrad["ACCOUNT_NME"] = strPromotionNme;
        //    drnewtrad["USER_TYPE_CID"] = "3";
        //    drnewtrad["PAY_DIR"] = "收入";//收付：收入、支出 
        //    drnewtrad["TRADING_SID"] = "已支付"; 
        //    drnewtrad["USER_NME"] = strUserNme;
        //    drnewtrad["PROMOTION_NME"] = strPromotionNme;
        //    drnewtrad["FWDUSER_NME"] = strFwdNme;
        //    drnewtrad["TRADING_PRICE"] = msBsc.SvRef.SysConst.decPromotionScorePrice;
        //    drnewtrad["TRADING_AMT"] = -(int)drnewtrad["TRADING_QTY"] * msBsc.SvRef.SysConst.decPromotionScorePrice; 
        //    drnewtrad["TRADING_DESC"] = "客户消费推广商获得积分";
        //    dstrad.Tables[0].Rows.Add(drnewtrad);
        //    //代理商增加积分
        //    DataRow drnewtrad2 = dstrad.Tables[0].NewRow();
        //    Epm.AppLogic.Framework.CUtil.CopyDataRow(drnewtrad, drnewtrad2);
        //    drnewtrad2["SCORE_PROMOTION_ID"] = this.DataObj.NewID();
        //    drnewtrad2["ACCOUNT_NO"] = strFwdNme;
        //    drnewtrad2["ACCOUNT_NME"] = strFwdNme;
        //    drnewtrad2["USER_TYPE_CID"] = "4";
        //    drnewtrad2["TRADING_DESC"] = "客户消费代理商获得积分";
        //    drnewtrad2["TRADING_SID"] = "已支付"; 
        //    dstrad.Tables[0].Rows.Add(drnewtrad2);


        //    this.DataObj.ExecuteBySQL("UPDATE A SET PROMOTION_SCORE_QTY=ISNULL(A.PROMOTION_SCORE_QTY,0)+" + drnewtrad["TRADING_QTY"].ToString()
        //                            + " ,PROMOTION_CHARGE_AMT=ISNULL(A.PROMOTION_CHARGE_AMT,0)+" + drnewtrad["TRADING_AMT"].ToString()
        //                            + " FROM SHP_CUSTOMER_INFO A WHERE A.USER_NME='" + strPromotionNme + "'");
        //    this.DataObj.ExecuteBySQL("UPDATE A SET PROMOTION_SCORE_QTY=ISNULL(A.PROMOTION_SCORE_QTY,0)+" + drnewtrad["TRADING_QTY"].ToString()
        //                            + " ,PROMOTION_CHARGE_AMT=ISNULL(A.PROMOTION_CHARGE_AMT,0)+" + drnewtrad["TRADING_AMT"].ToString()
        //                            + " FROM SHP_CUSTOMER_INFO A WHERE A.USER_NME='" + strFwdNme + "'"); 
        //    //}

        //    this.Update(dstrad);

        //    //}
        //}

         
    }
}
